package com.example.ch02_student.adminsDo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.ch02_student.R;
import com.example.ch02_student.entity.Student;
import com.example.ch02_student.utils.ResultVo;
import com.example.ch02_student.utils.UrlBase;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.Serializable;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class StudentSelfUpdateInfo extends AppCompatActivity {
    private EditText sno, sname, cclass, major, tel, college, age;
    private ImageView img;
    private OkHttpClient okHttpClient;
    private String[] imgList;
    private String xzimg = "2131099750";
    private RadioButton sex1,sex2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student_self_update_info);

        Intent intent = getIntent();
        Student student = (Student) intent.getSerializableExtra("student");
        init();
        okHttpClient=new OkHttpClient();
        //填充数据
        sno.setText(student.getSno());
        sname.setText(student.getSname());
        if ("男".equals(student.getSex())){
            sex1.setChecked(true);
        }else {
            sex2.setChecked(true);
        }
        cclass.setText(student.getCclass());
        major.setText(student.getMajor());
        tel.setText(student.getTel());
        college.setText(student.getCollege());
        age.setText(student.getAge() + "");


        //System.err.println(Integer.parseInt(student.getImg()));
        if (student.getImg() != null && !student.getImg().equalsIgnoreCase("null")) {
            img.setImageResource(Integer.parseInt(student.getImg()));

        }


        System.err.println(student);
    }

    //修改按钮
    public void update(View view) {

        Student student = new Student(
                sno.getText() + "", sname.getText() + "", xzimg, sex1.isChecked()?"男":"女",
                cclass.getText() + "", major.getText() + "", tel.getText() + ""
                , college.getText() + "", Integer.parseInt(age.getText() + ""), null
        );
        String s=new Gson().toJson(student);

        //post请求
        RequestBody requestBody = RequestBody.create(s, MediaType.parse("application/json"));
        Request build = new Request.Builder()
                .url(UrlBase.URL_BASE + "/studentSelf/updateStuSelf")
                .post(requestBody)
                .build();
        Call call = okHttpClient.newCall(build);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                if (string != null) {
                    ResultVo resultVo = new Gson().fromJson(string, ResultVo.class);
                    if (resultVo.getCode() == 1000) {
                        Looper.prepare();
                        Toast.makeText(StudentSelfUpdateInfo.this, "修改成功", Toast.LENGTH_SHORT).show();
                        Looper.loop();
                    } else {
                        Looper.prepare();
                        Toast.makeText(StudentSelfUpdateInfo.this, "修改失败", Toast.LENGTH_SHORT).show();
                        Looper.loop();
                    }
                }
            }

        });
    }

    public void back(View view) {
        startActivity(new Intent(StudentSelfUpdateInfo.this,StudentSelfInfo.class));
    }

    //初始化控件
    public void init() {
        sno = findViewById(R.id.sno);
        sname = findViewById(R.id.sname);
        sex1 = findViewById(R.id.sex1);
        sex2=findViewById(R.id.sex2);
        cclass = findViewById(R.id.cclass);
        major = findViewById(R.id.major);
        tel = findViewById(R.id.tel);
        college = findViewById(R.id.college);
        age = findViewById(R.id.age);

        img = findViewById(R.id.img);
    }
}
